package com.example.lenovo.weiyi;

import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;

import com.com.ruanmeng.demon.CommonM;
import com.com.ruanmeng.utils.CommonUtil;
import com.com.ruanmeng.utils.PreferencesUtils;
import com.yolanda.nohttp.NoHttp;
import com.yolanda.nohttp.rest.Request;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import nohttp.CallServer;
import nohttp.CustomHttpListener;
import share.Const;
import share.HttpIP;

/**
 * 更换手机号--密码
 */
public class ChangePhonePswActivity extends BaseActivity {

    @BindView(R.id.et_user_paw)
    EditText etUserPaw;
    @BindView(R.id.bt_queding)
    Button btQueding;
    @BindView(R.id.cb_is_visible)
    CheckBox cbIsVisible;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_changepgonepsw);
        ButterKnife.bind(this);
        AddActivity(this);
        ChangLayTitle("更换手机号");
        LayBack();
        init();
    }


    private void init() {
        etUserPaw.addTextChangedListener(this);
        btQueding.setText("下一步");
    }

    @Override
    public void onTextChanged(CharSequence s, int i, int i1, int i2) {
        super.onTextChanged(s, i, i1, i2);
        if (s.length() > 0) {
            cbIsVisible.setVisibility(View.VISIBLE);
            btQueding.setBackgroundResource(R.drawable.ed_fullmain);
            btQueding.setClickable(true);
        } else {
            cbIsVisible.setVisibility(View.GONE);
            btQueding.setBackgroundResource(R.drawable.ed_fullgeray);
            btQueding.setClickable(false);
        }
    }

    public void doClick(View v) {
        switch (v.getId()) {
            case R.id.bt_queding:
                if (TextUtils.isEmpty(etUserPaw.getText().toString())) {
                    CommonUtil.showToask(ChangePhonePswActivity.this, "请输入登录密码！");
                    return;
                }
                if (!etUserPaw.getText().toString().equals(PreferencesUtils.getString(ChangePhonePswActivity.this, "psw"))) {
                    CommonUtil.showToask(ChangePhonePswActivity.this, "请输入正确的登录密码！");
                    return;
                }
                getData();
                break;
            case R.id.cb_is_visible:
                if (cbIsVisible.isChecked()) {
                    etUserPaw.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                } else {
                    etUserPaw.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                }
                break;


        }
    }

    public void getData() {
        Request<String> mRequest = NoHttp.createStringRequest(HttpIP.ChangePhone_psw, Const.POST);
        mRequest.addHeader("token", PreferencesUtils.getString(ChangePhonePswActivity.this, "uid"));
        mRequest.add("password", etUserPaw.getText().toString());
        // 添加到请求队列
        CallServer.getRequestInstance().add(ChangePhonePswActivity.this, 0, mRequest,
                new CustomHttpListener(ChangePhonePswActivity.this, true, CommonM.class) {
                    @Override
                    public void doWork(Object data, boolean isOne) {
                        CommonM model = ((CommonM) data);
                        Intent in = new Intent(ChangePhonePswActivity.this, ChangePhonenumberActivity.class);
                        startActivity(in);
                    }

                    @Override
                    public void onFinally(JSONObject obj) throws JSONException {
                        super.onFinally(obj);
                        if (!("100").equals(obj.getString("msgcode"))) {
                            CommonUtil.showToask(ChangePhonePswActivity.this, obj.getString("msg"));
                        }
                    }
                }, true, true);
    }

}
